package cn.hznc.controller.service.order.logic;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.vo.BomParamVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Administrator
 * @Date 2023/3/22 11:17
 */
public class OrderBomLogic {
    private final OrderBomDo orderBomDo;

    private final List<BomParamVo> updateList;

    private final List<BomParamVo> originList;

    public void buildOrderBomDo() {
        // 清空业务模型
        if (CollectionUtil.isEmpty(updateList)){
            orderBomDo.addAllElement();
            return;
        }
        // 只需要更新
        if (CollectionUtil.isEmpty(originList)){
            orderBomDo.addCreateElement(updateList);
            orderBomDo.addAllElement();
            return;
        }
        // 比对code判断，对删除，修改，新增
        List<String> codeList = originList.stream().map(BomParamVo::getCode).collect(Collectors.toList());
        for (BomParamVo bomParamVo : updateList) {
            // 不包含就是新增
            if (!codeList.contains(bomParamVo.getCode())){
                orderBomDo.addCreateElement(bomParamVo);
            }else {
                orderBomDo.addUpdateElement(bomParamVo);
            }
        }
        orderBomDo.addAllElement();
    }

    public OrderBomLogic( List<BomParamVo> originList,List<BomParamVo> updateList) {
        this.updateList = updateList;
        this.originList = originList;
        this.orderBomDo=new OrderBomDo();
    }


    /**
     * 获取bom对象
     *
     * @return
     */
    public OrderBomDo getOrderBomDo() {
        return orderBomDo;
    }
}
